#!/usr/bin/env python

import os, sys, getopt
import csv, json

import utility


# given file index,
# return correspondence file name
def generate_file_name_by_index(index):
    file_name_prefix = "part-00"
    file_name_suffix = "-of-00500.csv"

    if index < 10:
        file_name = file_name_prefix+"00"+str(index)+file_name_suffix
    elif index < 100:
        file_name = file_name_prefix+"0"+str(index)+file_name_suffix
    else:
        file_name = file_name_prefix+str(index)+file_name_suffix
    return file_name


def write_job_events_to_file(job_events, output_prefix, hash_value_to_user_id):
    statistics = {}
    for (job_id, user, scheduling_class) in job_events:
        if user not in statistics:
            statistics[user] = {}
        if scheduling_class not in statistics[user]:
            statistics[user][scheduling_class] = []
        statistics[user][scheduling_class].append(job_id)

    for (user, scheduling_class) in statistics.items():
        if user in hash_value_to_user_id:
            folder_index = hash_value_to_user_id[user]
        else:
            folder_index = str(len(hash_value_to_user_id))
            folder_length = len(folder_index)
            if folder_length == 1:
                folder_index = "00"+folder_index
            elif folder_length == 2:
                folder_index = "0"+folder_index
            hash_value_to_user_id[user] = folder_index
            os.system("mkdir "+output_prefix+folder_index)

        for (single_class, job_records) in scheduling_class.items():
            target_file = output_prefix+folder_index+"/"+str(single_class)
            if os.path.exists(target_file):
                with open(target_file, "r") as f:
                    history_records = json.load(f)
                    job_records.extend(history_records)
            
            with open(target_file, "w") as f:
                json.dump(job_records, f)


# main framework that trigger the process
def process_data(begin_index, end_index):
    job_events_prefix = "job_events/"
    
    output_prefix = "output_job_events/"
    if not os.path.exists(output_prefix):
        os.system("mkdir "+output_prefix)

    hash_value_to_user_id = {}
    for i in range(begin_index, end_index):
        job_events_file_name = job_events_prefix+generate_file_name_by_index(i)
        job_events = utility.extract_job_events(job_events_file_name)
        write_job_events_to_file(job_events, output_prefix, hash_value_to_user_id)


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "hb:e:")
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)

    # set default value
    begin_index = 0
    end_index = 500

    for o, s in opts:
        # help information
        if o == "-h":
            print "usge   -b <begin package index>"
            print "       -e <end package index>"
            return
        elif o == "-b":
            begin_index = int(s)
        elif o == "-e":
            end_index = int(s)

    process_data(begin_index, end_index)
    

if __name__ == "__main__":
    main(sys.argv)
